const http = require('http')
const parse = require('url').parse
const routers = []
function createApplication() {
    return {
        // 收集路由
        get(path,handler) {
            routers.push( { path, method: 'get', handler})
        },
        listen() {
            const server = http.createServer((req, res) => {
                const {pathname} = parse(req.url)
                const method = req.method.toLowerCase()
                const router = routers.find(router => router.method === method && router.path === pathname)
                if(router){
                   return router.handler(req,res)
                }
                res.end('404 Not Found')
            })
            server.listen(...arguments)
        }
    }
}
module.exports = createApplication